#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

eps = 1e-8

def signal(*args):
    # Sroc
    df = args[0]
    n = args[1]
    factor_name = args[2]

    """
    N=13
    M=21
    EMAP=EMA(CLOSE,N)
    SROC=(EMAP-REF(EMAP,M))/REF(EMAP,M)
    SROC 与 ROC 类似，但是会对收盘价进行平滑处理后再求变化率。
    """
    ema = df['close'].ewm(n, adjust=False).mean()
    ref = ema.shift(2 * n)
    df[factor_name] = (ema - ref) / (ref + eps)

    return df
